﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tcpclient.h"
#include "common.h"
#include "msgapi.h"
#include <QMessageBox>
#include <QTcpSocket>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);

    userRegister = new UserRegister(ui->widget);
    userList = new UserList(ui->widget);
    attendanceFace =   new AttendanceFace(ui->widget);
    attendanceList = new AttendanceList(ui->widget);

    widgetList.push_back(userRegister);
    widgetList.push_back(userList);
    widgetList.push_back(attendanceFace);
    widgetList.push_back(attendanceList);

    hideAllWidget();

    // 服务器连接变化
    connect(TcpClient::getClient(), &TcpClient::connectChangeSignal, this, &MainWindow::connectChangeSlot);
    // 有数据可读
    connect(TcpClient::getClient()->getSocket(), &QTcpSocket::readyRead, this, &MainWindow::recvDataSlot);
    // 设置初始连接状态
    connectChangeSlot(TcpClient::getClient()->isConnected());
}

MainWindow::~MainWindow() {
    for(int i = 0; i < widgetList.size(); i++) {
        delete widgetList.at(i);
    }
    delete ui;
}

void MainWindow::hideAllWidget() {
    for(int i = 0; i < widgetList.size(); i++) {
        widgetList.at(i)->hide();
        if(widgetList.at(i)->objectName() == "AttendanceFace") {
            attendanceFace->stopCamera();
        } else if(widgetList.at(i)->objectName() == "UserRegister") {
            userRegister->stopCamera();
        }
    }
}

void MainWindow::showAssignWidget(QString objectName) {
    for(int i = 0; i < widgetList.size(); i++) {
        if(objectName == widgetList.at(i)->objectName()) {
            widgetList.at(i)->show();
            // 不能用一个摄像头在两个页面同时显示,开一个页面的时候要关另一个页面的摄像头
            if(widgetList.at(i)->objectName() == "AttendanceFace") {
                ((AttendanceFace *)widgetList.at(i))->startCamera();
            } else if(widgetList.at(i)->objectName() == "UserRegister") {
                ((UserRegister *)widgetList.at(i))->startCamera();
            }
            break;
        }
    }
}

void MainWindow::connectChangeSlot(bool isConnect) {
    if(isConnect) {
        ui->connectStatusTextLabel->setText("已连接");
        ui->connectStatusTextLabel->setStyleSheet("color:green;");
    } else {
        ui->connectStatusTextLabel->setText("未连接");
        ui->connectStatusTextLabel->setStyleSheet("color:red;");
    }
}

void MainWindow::recvDataSlot() {
    QByteArray data = TcpClient::getClient()->getSocket()->readAll();
    qDebug() << "recvDataSlot data:" << data;

    // json解析
    QJsonDocument doc = QJsonDocument::fromJson(data);
    if(!doc.isObject()) {
        qDebug() << "recvDataSlot data not json object";
        return ;
    }
    QJsonObject object = doc.object();
    int type = object.value("type").toInt();

    switch (type) {
        case MSG_EXIT: {
            break;
        }
        case MSG_QUIT: {
            break;
        }
        case MSG_USER_REGISTER: {
            int result;
            int count;
            if(!MsgApi::getMsgApi()->parseUserRegister(object, result, count)) {
                qDebug() << "MSG_USER_INFO_LIST parseUserRegister error";
                QMessageBox::warning(this, "注册失败", "无法解析返回数据");
                break;
            }
            if(result == MSG_RESULT_SUCCESS) {
                userList->setPageShow(count);
                userList->refreshUserList();  // 刷新页面
                QMessageBox::warning(this, "注册成功", "请在用户列表查看");
            } else if(result == MSG_RESULT_ERROR) {
                QMessageBox::warning(this, "注册失败", "插入数据失败");
            }
            break;
        }
        case MSG_USER_INFO_LIST: {
            int count;
            QList<UInfo> userInfoList;
            if(!MsgApi::getMsgApi()->parseGetUserInfoList(object, count, userInfoList)) {
                qDebug() << "MSG_USER_INFO_LIST parseGetUserInfoList error";
                break;
            }

            userList->setPageShow(count);
            userList->showUserList(userInfoList);
            break;
        }
        case MSG_USER_DELETE: {
            int result;
            int count;
            if(!MsgApi::getMsgApi()->parseUserDelete(object, result, count)) {
                qDebug() << "MSG_USER_DELETE parseUserDelete error";
                QMessageBox::warning(this, "删除失败", "无法解析返回数据");
                break;
            }
            if(result == MSG_RESULT_SUCCESS) {
                userList->setPageShow(count);
                userList->refreshUserList();  // 刷新页面
                QMessageBox::warning(this, "删除成功", "请在用户列表查看");
            } else if(result == MSG_RESULT_ERROR) {
                QMessageBox::warning(this, "删除失败", "插入数据失败");
            }
            break;
        }
        case MSG_ATT_INSERT_RECORD: {
            int result;
            int count;
            QString createTime;
            if(!MsgApi::getMsgApi()->parseInsertAttendanceRecord(object, result, count, createTime)) {
                qDebug() << "MSG_ATT_INSERT_RECORD parseInsertAttendanceRecord error";
                break;
            }
            if(result == MSG_RESULT_SUCCESS) {
                attendanceList->setPageShow(count);
                attendanceList->refreshAttendanceList();
                QMessageBox::warning(this, "打卡成功", "打卡时间:" + createTime);
            } else if(result == MSG_RESULT_ERROR) {
                QMessageBox::warning(this, "打卡失败", "打卡时间:" + createTime);
            }
            break;
        }
        case MSG_ATT_INFO_LIST: {
            int count;
            QList<AInfo> attendanceInfoList;
            if(!MsgApi::getMsgApi()->parsegetAttendanceInfoList(object, count, attendanceInfoList)) {
                qDebug() << "MSG_ATT_INFO_LIST parsegetAttendanceInfoList error";
                break;
            }
            attendanceList->setPageShow(count);
            attendanceList->showAttendanceList(attendanceInfoList);
            break;
        }
    }
}

void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem * item, int column) {
    QString whatsThis = item->whatsThis(column);
    hideAllWidget();
    showAssignWidget(whatsThis);
}
